<html>
<head>
<title>BusinessData Services</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<link rel="stylesheet" type="text/css" href="services.css"/>
</head>
<body>

<h1>Smart Callsign Search Services</h1>
<div class="hostInfo" >
<form action="">
<div>
<label for="scheme">Transport</label><select name="scheme" id="scheme">
<option value="http://">HTTP</option>
<option value="https://">HTTPS</option></select>
</div>
<div>
<label for="host">Host</label><select name="host" id="host"></select>
</div>
<div>
<label for="method">Method</label>GET<input type="hidden" name="method" id="method" value="GET">
</div>
<div>
<label for="dialog">Force save dialog</label><input type="checkbox" id="forceDialog" name="forceDialog" checked="checked"/>
</div>
</form>
</div>
<div class="services">
<div class="serviceInfo">
<form id="callsign_search" class="serviceForm">
<h2>Callsign Search</h2>
<div>
<label for="url">Base Url:</label>
<input type="text" name="url" value="/data/app/ws/rest/rsm/certificatesAndCallsigns/search/"/>
</div>
<div>
<label for="version">Version:</label>

<select name="version">
<option value="v1.0">v1.0</option>
</select>
</div>
<div>
<label for="type">Criteria Type:</label>

<select name="type">
<option value="callsign">Callsign</option>
<option value="certificateNumber">Certificate Number</option>
<option value="clientId">Client Id</option>
<option value="clientName">Client Name</option>
<option value="vesselName">Vessel Name</option>
<option value="mmsi">MMSI</option>
<option value="rtn">RTN</option>
</select>
</div>
<div>
<label for="suffix">Query:</label>
<input type="text" name="suffix" value=""/>
</div>
<div>
<label for="serviceUrl">Service URL:</label>
<span class="action"></span>
</div>
<button onclick="dosubmit(this.form);return false;">Submit</button>
</form>
</div>

</div>
</div>
<form id="actionform" target="_blank"></form>
<script type="text/javascript">
var topWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                   .getInterface(Components.interfaces.nsIWebNavigation)
                   .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
                   .rootTreeItem
                   .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                   .getInterface(Components.interfaces.nsIDOMWindow);
var BusinessDataChrome = topWindow.BusinessDataChrome; 

function dosubmit(form){
   var $host = $('#host');
   var $scheme = $('#scheme option:selected');
   var $method = $('#method');
   var $form = $('#actionform'); 
   $form.attr('method', $method.val());
   var $action = $('span.action', form);
   $form.attr('action', $scheme.val()+$host.val()+$action.text());
   $form.get(0).submit();
}

$(function(){
  BusinessDataChrome.initAccessInfo();
  var sel = $('#host').get(0);
  for(key in BusinessDataChrome.accessInfo){
    var op = new Option(key, key,false);
    sel.options.add(op);
  }
  $('#forceDialog').change(function(){
    BusinessDataChrome.forceSaveDialog = $('#forceDialog:checked').length >0
    }
  );
  $('#host').change(function(){
    $('form.serviceForm').each(function(ind){
        makeUrl(ind)
    });
  });
  $('form.serviceForm').each(function(ind){
    makeUrl(this);
    $(this).change(function(){
        makeUrl(this);
    });
  });
});


function makeUrl(frm){
    var $host = $('#host option:selected');
    var $url = $('input[name=url]', frm);
    var $version = $('select[name=version] option:selected', frm);
    var $type = $('select[name=type] option:selected', frm);
    var $suffix = $('input[name=suffix]', frm);
    var uri = $url.val()+$version.val()+'/'+$type.val()+'/'+escape($suffix.val());
   // alert($host.val());
    if ($host.val() == 'localhost'){
        uri = uri.replace(/\/data\/app\/ws/, ':8080/fmit-g2b-webservice');
    }
    //alert(uri);
    $('span.action',frm).html(uri);
}
</script>
</body>
</html>
